<?php
echo $this->Session->flash();
App::Import('model', 'TopographicMap');
App::Import('model', 'TopographicArea');

$totalAreaPlano = 0;
$totalAreaUtil = 0;
$totalAreaNoUtil = 0;
?>

<div id="loading" style="display: none;">
    <?php echo $html->image('loading.gif', array('border' => "0", 'align' => 'center')); ?>
</div>

<h1>PREDIOS PROYECTO <?php echo$cod; ?></h1>
<table id="tabla" >
    <thead>
        <tr>
            <th>Código</th>
            <th>Nombre</th>
            <th>Área total (ha)</th>
            <th>Área útil (ha)</th>
            <th>%</th>
            <th>Área no útil (ha)</th>
            <th>%</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($properties as $prop):
            //Busco el ultimo plano topografico y lo sumo a la variable $totalAreaPlano para luego mostrar el total del area de los planos del proyecto.
            $planos = new TopographicMap();
            $planos->recursive = -1;
            $plano = $planos->find('first', array('conditions' => array('TopographicMap.property_id' => $prop['Property']['id']), 'order' => array('TopographicMap.id DESC'), 'fields' => array('TopographicMap.id', 'TopographicMap.area')));
            //sumo al total del proyecto
            $totalAreaPlano += $plano['TopographicMap']['area'];
            //Traigo el total de las areas utiles y no utiles
            $areas = new TopographicArea();
            $areas->recursive = 0;
            $areaTotalUtil = $areas->field('Sum(TopographicArea.magnitud)', array('TopographicArea.topographic_map_id' => $plano['TopographicMap']['id'], 'AreaType.productiva' => 1));
            $areaTotalNoUtil = $areas->field('Sum(TopographicArea.magnitud)', array('TopographicArea.topographic_map_id' => $plano['TopographicMap']['id'], 'AreaType.productiva' => 0));
            $totalAreaNoUtil += $areaTotalNoUtil;
            $totalAreaUtil += $areaTotalUtil;

            $error = (( round(($areaTotalUtil + $areaTotalNoUtil), 4) != round($plano['TopographicMap']['area'], 4) ) or $plano['TopographicMap']['area'] < 0 or empty($plano['TopographicMap']['area']) ) ? true : false;
            ?>
            <tr>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8;'") ?>>
                    <?php echo $prop['Property']['codigo'] ?>
                </td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> >
                    <?php echo $prop['Property']['nombre'] ?></td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?>>
                    <?php echo number_format($plano['TopographicMap']['area'], 4, ',', '.') ?></td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> >
                    <?php echo number_format($areaTotalUtil, 4, ',', '.') ?></td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?>>
                    <?php echo (($plano['TopographicMap']['area'] > 0) ? number_format(($areaTotalUtil * 100) / ($plano['TopographicMap']['area']), 2, ',', '.') . "%" : "" ) ?></td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> >
                    <?php echo number_format($areaTotalNoUtil, 4, ',', '.') ?></td>
                <td <?php echo ($error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> >
                    <?php echo (($plano['TopographicMap']['area'] > 0) ? number_format(($areaTotalNoUtil * 100) / ($plano['TopographicMap']['area']), 2, ',', '.') . "%" : "" ) ?></td>
                <td>
                    <br>                    
                    <?php echo $ajax->link('Editar', array('controller' => 'topographicMaps', 'action' => 'index', $prop['Property']['id']), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading')); ?>
                    <br><br>
                </td>
            </tr>
        <?php endforeach; ?>
        <tr>
            <td></td>
            <td><b>Área total (ha)</b></td>
            <td><?php echo number_format($totalAreaPlano, 4, ',', '.') . ' (ha)' ?></td>
            <td><?php echo number_format($totalAreaUtil, 4, ',', '.') . ' (ha)' ?></td>
            <td><?php echo ($totalAreaPlano > 0) ? number_format((($totalAreaUtil * 100) / $totalAreaPlano), 2, ',', '.') . ' %' : "0 %" ?></td>
            <td><?php echo number_format($totalAreaNoUtil, 4, ',', '.') . ' (ha)' ?></td>
            <td><?php echo ($totalAreaPlano > 0) ? number_format((($totalAreaNoUtil * 100) / $totalAreaPlano), 2, ',', '.') . ' %' : "0 %" ?></td>
            <td></td>
        </tr>
    </tbody>
</table>
<br>